package com.lyl.security.common.sercurity.handler;

import com.lyl.security.common.result.ErrorCode;
import com.lyl.security.common.result.Result;
import com.lyl.security.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Component
public class AccessFailureHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        log.error("权限不足", e);
        ServletUtils.write(response, Result.build(ErrorCode.ACCESS_DENIED, e.getMessage()));
    }
}
